package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.PushHistoryRepository;
import com.hualude.roadsign.repository.SolarFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.SolarEnergyCheckService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ding on 2017/7/10.
 */
@Service
public class SolarEnergyCheckServiceImpl implements SolarEnergyCheckService {

    @Resource
    private SolarFaultRepository solarFaultRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DTResponseDto getSolarFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<DTSolarFaultDto> dto4DTs = solarFaultRepository.getSolarFault4DT(dtRequestDto, actorId, orgId);
        Integer count = solarFaultRepository.getSolarFaultCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for (DTSolarFaultDto dtSolarFaultDto : dto4DTs) {
            String solarCreatorName = userRepository.getUserNameByUserId(dtSolarFaultDto.getSolarCreatorId());
            dtSolarFaultDto.setSolarCreatorName(solarCreatorName);
            String solarCheckerName = userRepository.getUserNameByUserId(dtSolarFaultDto.getSolarCheckerId());
            dtSolarFaultDto.setSolarCheckerName(solarCheckerName);
            if (dtSolarFaultDto.getSolarMainCheckerId() != null) {
                String solarMainCheckerName = userRepository.getUserNameByUserId(dtSolarFaultDto.getSolarMainCheckerId());
                dtSolarFaultDto.setSolarMainCheckerName(solarMainCheckerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer solarFaultAudit(Integer isPass, String solarFeedback, Integer solarFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        if (isPass == 1||isPass==2) {
            final String[] strFaultType = {"损毁", "其他"};
            final SolarEnergyFaultDto faultDto = solarFaultRepository.getInfoByFaultId(solarFaultId);
            final Integer orgId = faultDto.getSolarOrgId();
            List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
            final String msg = "太阳能 NO：" + faultDto.getSolarEnergyId() + " " +
                    strFaultType[faultDto.getSolarType()];
            strUserIds = "";
            for (UserDto userDto : userDtos) {
                strUserIds += userDto.getUserId().toString() + ",";
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!strUserIds.equals("")) {
                            strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                            PushUtil.sendPushToUser(strUserIds, msg);
                            PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),4);
                            pushHistoryRepository.save(pushHistoryDto.toBean());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            if(isPass==1)
                return solarFaultRepository.solarFaultAudit2Success(isPass, solarFeedback, solarFaultId, userId, dateNowStr);
            else
                return solarFaultRepository.straightFaultAudit(isPass, solarFeedback, solarFaultId, userId, dateNowStr);
        } else
            return solarFaultRepository.solarFaultAudit2Fail(isPass, solarFeedback, solarFaultId, userId, dateNowStr);
    }

    public List<SolarEnergyFaultDto> getSolarFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus,
                                                                Integer count, Integer size) throws Exception {
        return solarFaultRepository.getSolarFaultCheckList4App(orgId, actorIds, faultStatus, count, size);
    }

    @Transactional
    public Integer solarFaultCheck(Integer isPass, String solarFeedback, Integer solarFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        if (solarFaultRepository.getSolarIsCheck(solarFaultId) == 1)//若企业负责人已审核,主管负责人审核
        {
            if (isPass == 1) {
                final String[] strFaultType = {"损毁", "其他"};
                final SolarEnergyFaultDto faultDto = solarFaultRepository.getInfoByFaultId(solarFaultId);
                final Integer orgId = faultDto.getSolarOrgId();
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "太阳能 NO：" + faultDto.getSolarEnergyId() + " " +
                        strFaultType[faultDto.getSolarType()];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),4);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                return solarFaultRepository.solarFaultAudit2Success(isPass, solarFeedback, solarFaultId, userId, dateNowStr);
            } else {
                return solarFaultRepository.solarFaultAudit2Fail(isPass, solarFeedback, solarFaultId, userId, dateNowStr);
            }
        } else {
            if (isPass == 1) {
                final SolarEnergyFaultDto faultDto = solarFaultRepository.getInfoByFaultId(solarFaultId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(faultDto.getSolarMainOrgId(), 7);
                final String msg = "太阳能 NO：" + faultDto.getSolarEnergyId() + " 待审核";
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(faultDto.getSolarMainOrgId(), 7, msg, formatter.format(new Date()),4);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            return solarFaultRepository.solarFaultAudit(isPass, solarFeedback, solarFaultId, userId, dateNowStr);
        }
    }
}
